/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VideoDes.java
 *
 * Created on Jun 21, 2011, 6:29:19 PM
 */
package cz.muni.fi.pb138.videodes.desktop;

import cz.muni.fi.pb138.videodes.api.base.XMLDBTemplate;
import cz.muni.fi.pb138.videodes.entity.Dvd;
import cz.muni.fi.pb138.videodes.entity.DvdType;
import cz.muni.fi.pb138.videodes.entity.Genre;
import cz.muni.fi.pb138.videodes.entity.Movie;
import cz.muni.fi.pb138.videodes.impl.DvdManager;
import cz.muni.fi.pb138.videodes.impl.DvdManagerImpl;
import cz.muni.fi.pb138.videodes.impl.ExportToODS;
import cz.muni.fi.pb138.videodes.impl.ImportFailedException;
import cz.muni.fi.pb138.videodes.impl.ImportFromODS;
import cz.muni.fi.pb138.videodes.impl.MovieManager;
import cz.muni.fi.pb138.videodes.impl.MovieManagerImpl;
import cz.muni.fi.pb138.videodes.impl.VideoDesDriver;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingWorker;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.log4j.xml.DOMConfigurator;

/**
 * Class with main method, creates GUI.
 * 
 * @author vaclav
 */
public class VideoDes extends javax.swing.JFrame {

    private MovieManager movieManager = new MovieManagerImpl();
    private DvdManager dvdManager = new DvdManagerImpl();
    private static final Logger LOGGER = Logger.getLogger(VideoDes.class.getName());

    /** Creates new form VideoDes */
    public VideoDes() {
        initComponents();

        XMLDBTemplate.setDriver(new VideoDesDriver());
        XMLDBTemplate.open();

        new RetrieveDvds().execute();
        new RetrieveMovies().execute();
    }

    /**
     * This method creates directory, if the specified directory
     * does not exist
     * @param directoryName the name of the directory
     */
    private static void createDirectoryIfDoesntExist(String directoryName) {
        File directory = new File(directoryName);

        // if the directory does not exist, create it
        if (!directory.exists()) {
            directory.mkdir();
        }
    }

    /** 
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenuTableDvd = new javax.swing.JPopupMenu();
        jMenuPopupNewDvd = new javax.swing.JMenuItem();
        jSeparatorPopup = new javax.swing.JPopupMenu.Separator();
        jMenuPopupDetailDvd = new javax.swing.JMenuItem();
        jSeparatorPopup2 = new javax.swing.JPopupMenu.Separator();
        jMenuPopupEditDvd = new javax.swing.JMenuItem();
        jMenuPopupDeleteDvd = new javax.swing.JMenuItem();
        jFrameManageMovies = new javax.swing.JFrame();
        jToolBarMM = new javax.swing.JToolBar();
        jButtonNewMovie = new javax.swing.JButton();
        jButtonEditMovie = new javax.swing.JButton();
        jButtonDeleteMovie = new javax.swing.JButton();
        jScrollPaneMovieTable = new javax.swing.JScrollPane();
        jTableMovie = new javax.swing.JTable();
        jButtonMMClose = new javax.swing.JButton();
        jPopupMenuTableMovie = new javax.swing.JPopupMenu();
        jMenuPopupNewMovie = new javax.swing.JMenuItem();
        jSeparatorPopupMM = new javax.swing.JPopupMenu.Separator();
        jMenuPopupEditMovie = new javax.swing.JMenuItem();
        jMenuPopupDeleteMovie = new javax.swing.JMenuItem();
        jFrameNewEditMovie = new javax.swing.JFrame();
        jTextFieldMovieName = new javax.swing.JTextField();
        jLabelMovieName = new javax.swing.JLabel();
        jTextFieldMovieDirector = new javax.swing.JTextField();
        jLabelMovieDirector = new javax.swing.JLabel();
        jSpinnerMovieYear = new javax.swing.JSpinner();
        jLabelMovieYear = new javax.swing.JLabel();
        jTextFieldMovieCountry = new javax.swing.JTextField();
        jLabelMovieCountry = new javax.swing.JLabel();
        jLabelMovieActors = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListMovieActors = new javax.swing.JList();
        jButtonMovieDeleteActor = new javax.swing.JButton();
        jButtonMovieAddActor = new javax.swing.JButton();
        jLabelMovieGenres = new javax.swing.JLabel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListMovieGenresNotIn = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jListMovieGenresIn = new javax.swing.JList();
        jButtonMovieConfirm = new javax.swing.JButton();
        jButtonMovieClose = new javax.swing.JButton();
        jFrameDetailDvd = new javax.swing.JFrame();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTableDetailDvd = new javax.swing.JTable();
        jButtonDetailDvdClose = new javax.swing.JButton();
        jFrameNewEditDvd = new javax.swing.JFrame();
        jTextFieldDvdTitle = new javax.swing.JTextField();
        jLabelDvdTitle = new javax.swing.JLabel();
        jComboBoxDvdType = new javax.swing.JComboBox();
        jLabelDvdType = new javax.swing.JLabel();
        jButtonNewEditDvdConfirm = new javax.swing.JButton();
        jButtonNewEditDvdCancel = new javax.swing.JButton();
        jButtonNewEditDvdMM = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jScrollPane5 = new javax.swing.JScrollPane();
        jListNewEditDvdIn = new javax.swing.JList();
        jScrollPane6 = new javax.swing.JScrollPane();
        jListNewEditDvdNotIn = new javax.swing.JList();
        jFileChooserImport = new javax.swing.JFileChooser();
        jFileChooserExport = new javax.swing.JFileChooser();
        jToolBar = new javax.swing.JToolBar();
        jButtonNewDvd = new javax.swing.JButton();
        jButtonEditDvd = new javax.swing.JButton();
        jButtonDeleteDvd = new javax.swing.JButton();
        jSeparatorToolBar = new javax.swing.JToolBar.Separator();
        jButtonDetailDvd = new javax.swing.JButton();
        jSeparatorToolBar2 = new javax.swing.JToolBar.Separator();
        jButtonManageMovies = new javax.swing.JButton();
        jScrollPaneDvdTable = new javax.swing.JScrollPane();
        jTableDvd = new javax.swing.JTable();
        jButtonSearch = new javax.swing.JButton();
        jComboBoxType = new javax.swing.JComboBox();
        jTextFieldSearch = new javax.swing.JTextField();
        jMenuBar = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuItemNewDvd = new javax.swing.JMenuItem();
        jSeparatorFile = new javax.swing.JPopupMenu.Separator();
        jMenuItemDetailDvd = new javax.swing.JMenuItem();
        jMenuItemImport = new javax.swing.JMenuItem();
        jMenuItemExport = new javax.swing.JMenuItem();
        jSeparatorFile2 = new javax.swing.JPopupMenu.Separator();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenuEdit = new javax.swing.JMenu();
        jMenuItemEditDvd = new javax.swing.JMenuItem();
        jMenuItemDeleteDvd = new javax.swing.JMenuItem();

        jMenuPopupNewDvd.setText("New");
        jMenuPopupNewDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPopupNewDvdActionPerformed(evt);
            }
        });
        jPopupMenuTableDvd.add(jMenuPopupNewDvd);
        jPopupMenuTableDvd.add(jSeparatorPopup);

        jMenuPopupDetailDvd.setText("Detail (content)");
        jMenuPopupDetailDvd.setEnabled(false);
        jMenuPopupDetailDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPopupDetailDvdActionPerformed(evt);
            }
        });
        jPopupMenuTableDvd.add(jMenuPopupDetailDvd);
        jPopupMenuTableDvd.add(jSeparatorPopup2);

        jMenuPopupEditDvd.setText("Edit");
        jMenuPopupEditDvd.setEnabled(false);
        jMenuPopupEditDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPopupEditDvdActionPerformed(evt);
            }
        });
        jPopupMenuTableDvd.add(jMenuPopupEditDvd);

        jMenuPopupDeleteDvd.setText("Delete");
        jMenuPopupDeleteDvd.setEnabled(false);
        jMenuPopupDeleteDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPopupDeleteDvdActionPerformed(evt);
            }
        });
        jPopupMenuTableDvd.add(jMenuPopupDeleteDvd);

        jFrameManageMovies.setTitle("Manage movies");
        jFrameManageMovies.setMinimumSize(new java.awt.Dimension(600, 350));

        jToolBarMM.setFloatable(false);
        jToolBarMM.setRollover(true);

        jButtonNewMovie.setText("New");
        jButtonNewMovie.setFocusable(false);
        jButtonNewMovie.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonNewMovie.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonNewMovie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewMovieActionPerformed(evt);
            }
        });
        jToolBarMM.add(jButtonNewMovie);

        jButtonEditMovie.setText("Edit");
        jButtonEditMovie.setEnabled(false);
        jButtonEditMovie.setFocusable(false);
        jButtonEditMovie.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonEditMovie.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonEditMovie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditMovieActionPerformed(evt);
            }
        });
        jToolBarMM.add(jButtonEditMovie);

        jButtonDeleteMovie.setText("Delete");
        jButtonDeleteMovie.setEnabled(false);
        jButtonDeleteMovie.setFocusable(false);
        jButtonDeleteMovie.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonDeleteMovie.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonDeleteMovie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteMovieActionPerformed(evt);
            }
        });
        jToolBarMM.add(jButtonDeleteMovie);

        jTableMovie.setModel(new MovieTableModel());
        jTableMovie.setAutoCreateRowSorter(true);

        jTableMovie.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent lse) {
                if (!jTableDvd.getSelectionModel().isSelectionEmpty()) {
                    jButtonEditMovie.setEnabled(true);
                    jButtonDeleteMovie.setEnabled(true);
                    jMenuPopupDeleteMovie.setEnabled(true);
                    jMenuPopupEditMovie.setEnabled(true);
                } else {
                    jButtonEditMovie.setEnabled(true);
                    jButtonDeleteMovie.setEnabled(true);
                    jMenuPopupDeleteMovie.setEnabled(true);
                    jMenuPopupEditMovie.setEnabled(true);
                }
            }
        });

        jTableMovie.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                mousePressed(e);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    int selectedRow = jTableMovie.rowAtPoint(e.getPoint());
                    jTableMovie.setRowSelectionInterval(selectedRow, selectedRow);
                    jPopupMenuTableMovie.show(jTableMovie, e.getX(), e.getY());
                }
            }
        });
        jScrollPaneMovieTable.setViewportView(jTableMovie);

        jButtonMMClose.setText("Close");
        jButtonMMClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMMCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jFrameManageMoviesLayout = new javax.swing.GroupLayout(jFrameManageMovies.getContentPane());
        jFrameManageMovies.getContentPane().setLayout(jFrameManageMoviesLayout);
        jFrameManageMoviesLayout.setHorizontalGroup(
            jFrameManageMoviesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBarMM, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrameManageMoviesLayout.createSequentialGroup()
                .addContainerGap(547, Short.MAX_VALUE)
                .addComponent(jButtonMMClose)
                .addContainerGap())
            .addGroup(jFrameManageMoviesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPaneMovieTable, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
                .addContainerGap())
        );
        jFrameManageMoviesLayout.setVerticalGroup(
            jFrameManageMoviesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrameManageMoviesLayout.createSequentialGroup()
                .addComponent(jToolBarMM, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPaneMovieTable, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonMMClose)
                .addContainerGap())
        );

        jMenuPopupNewMovie.setText("New");
        jMenuPopupNewMovie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPopupNewMovieActionPerformed(evt);
            }
        });
        jPopupMenuTableMovie.add(jMenuPopupNewMovie);
        jPopupMenuTableMovie.add(jSeparatorPopupMM);

        jMenuPopupEditMovie.setText("Edit");
        jMenuPopupEditMovie.setEnabled(false);
        jMenuPopupEditMovie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPopupEditMovieActionPerformed(evt);
            }
        });
        jPopupMenuTableMovie.add(jMenuPopupEditMovie);

        jMenuPopupDeleteMovie.setText("Delete");
        jMenuPopupDeleteMovie.setEnabled(false);
        jMenuPopupDeleteMovie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPopupDeleteMovieActionPerformed(evt);
            }
        });
        jPopupMenuTableMovie.add(jMenuPopupDeleteMovie);

        jFrameNewEditMovie.setTitle("Manage movie");
        jFrameNewEditMovie.setMinimumSize(new java.awt.Dimension(500, 450));

        jLabelMovieName.setText("Name:");

        jLabelMovieDirector.setText("Director:");

        jSpinnerMovieYear.setValue(2000);

        jLabelMovieYear.setText("Year:");

        jLabelMovieCountry.setText("Country:");

        jLabelMovieActors.setText("Actors:");

        jListMovieActors.setModel(new ActorListModel());
        jScrollPane1.setViewportView(jListMovieActors);

        jButtonMovieDeleteActor.setText("Delete");
        jButtonMovieDeleteActor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMovieDeleteActorActionPerformed(evt);
            }
        });

        jButtonMovieAddActor.setText("Add");
        jButtonMovieAddActor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMovieAddActorActionPerformed(evt);
            }
        });

        jLabelMovieGenres.setText("Genres (drag and drop, genres on the right will be in this movie):");

        jSplitPane1.setResizeWeight(0.5);

        jListMovieGenresNotIn.setModel(new DefaultListModel());
        jListMovieGenresNotIn.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jListMovieGenresNotIn.setTransferHandler(new cz.muni.fi.pb138.videodes.desktop.GenreListTransferHandler());
        jListMovieGenresNotIn.setDragEnabled(true);
        jListMovieGenresNotIn.setDropMode(DropMode.INSERT);
        jScrollPane2.setViewportView(jListMovieGenresNotIn);

        jSplitPane1.setLeftComponent(jScrollPane2);

        jListMovieGenresIn.setModel(new DefaultListModel());
        jListMovieGenresIn.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jListMovieGenresIn.setTransferHandler(new cz.muni.fi.pb138.videodes.desktop.GenreListTransferHandler());
        jListMovieGenresIn.setDragEnabled(true);
        jListMovieGenresIn.setDropMode(DropMode.INSERT);
        jScrollPane3.setViewportView(jListMovieGenresIn);

        jSplitPane1.setRightComponent(jScrollPane3);

        jButtonMovieConfirm.setText("Confirm");
        jButtonMovieConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMovieConfirmActionPerformed(evt);
            }
        });

        jButtonMovieClose.setText("Cancel");
        jButtonMovieClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMovieCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jFrameNewEditMovieLayout = new javax.swing.GroupLayout(jFrameNewEditMovie.getContentPane());
        jFrameNewEditMovie.getContentPane().setLayout(jFrameNewEditMovieLayout);
        jFrameNewEditMovieLayout.setHorizontalGroup(
            jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrameNewEditMovieLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
                    .addComponent(jLabelMovieGenres)
                    .addGroup(jFrameNewEditMovieLayout.createSequentialGroup()
                        .addGroup(jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelMovieDirector)
                            .addComponent(jLabelMovieName)
                            .addComponent(jLabelMovieYear)
                            .addComponent(jLabelMovieCountry)
                            .addComponent(jLabelMovieActors)
                            .addGroup(jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jButtonMovieAddActor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonMovieDeleteActor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                            .addComponent(jTextFieldMovieCountry, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                            .addComponent(jSpinnerMovieYear, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                            .addComponent(jTextFieldMovieName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                            .addComponent(jTextFieldMovieDirector, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrameNewEditMovieLayout.createSequentialGroup()
                        .addComponent(jButtonMovieClose)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonMovieConfirm)))
                .addContainerGap())
        );
        jFrameNewEditMovieLayout.setVerticalGroup(
            jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrameNewEditMovieLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldMovieName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelMovieName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldMovieDirector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelMovieDirector))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSpinnerMovieYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelMovieYear))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldMovieCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelMovieCountry))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jFrameNewEditMovieLayout.createSequentialGroup()
                        .addComponent(jLabelMovieActors)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonMovieAddActor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonMovieDeleteActor))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelMovieGenres)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jFrameNewEditMovieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonMovieConfirm)
                    .addComponent(jButtonMovieClose))
                .addContainerGap())
        );

        jFrameDetailDvd.setMinimumSize(new java.awt.Dimension(600, 400));

        jTableDetailDvd.setModel(new MovieTableModel());
        jTableDetailDvd.setAutoCreateRowSorter(true);
        jScrollPane4.setViewportView(jTableDetailDvd);

        jButtonDetailDvdClose.setText("Close");
        jButtonDetailDvdClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDetailDvdCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jFrameDetailDvdLayout = new javax.swing.GroupLayout(jFrameDetailDvd.getContentPane());
        jFrameDetailDvd.getContentPane().setLayout(jFrameDetailDvdLayout);
        jFrameDetailDvdLayout.setHorizontalGroup(
            jFrameDetailDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrameDetailDvdLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jFrameDetailDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
                    .addComponent(jButtonDetailDvdClose))
                .addContainerGap())
        );
        jFrameDetailDvdLayout.setVerticalGroup(
            jFrameDetailDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrameDetailDvdLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonDetailDvdClose)
                .addContainerGap())
        );

        jFrameNewEditDvd.setTitle("Manage DVD");
        jFrameNewEditDvd.setMinimumSize(new java.awt.Dimension(500, 300));

        jLabelDvdTitle.setText("Title:");

        jComboBoxDvdType.setModel(new javax.swing.DefaultComboBoxModel(new DvdType[] { DvdType.COPY, DvdType.HOME, DvdType.MAGAZINE, DvdType.ORIGINAL }));

        jLabelDvdType.setText("Type:");

        jButtonNewEditDvdConfirm.setText("Confirm");
        jButtonNewEditDvdConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewEditDvdConfirmActionPerformed(evt);
            }
        });

        jButtonNewEditDvdCancel.setText("Cancel");
        jButtonNewEditDvdCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewEditDvdCancelActionPerformed(evt);
            }
        });

        jButtonNewEditDvdMM.setText("Manage movies");
        jButtonNewEditDvdMM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewEditDvdMMActionPerformed(evt);
            }
        });

        jLabel1.setText("Content (drag and drop, movies on the right will be on this DVD):");

        jSplitPane2.setResizeWeight(0.5);

        jListNewEditDvdIn.setModel(new MovieListModel());
        jListNewEditDvdIn.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jListNewEditDvdIn.setTransferHandler(new MovieListTransferHandler());
        jListNewEditDvdIn.setDragEnabled(true);
        jListNewEditDvdIn.setDropMode(DropMode.INSERT);
        jScrollPane5.setViewportView(jListNewEditDvdIn);

        jSplitPane2.setRightComponent(jScrollPane5);

        jListNewEditDvdNotIn.setModel(new MovieListModel());
        jListNewEditDvdNotIn.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jListNewEditDvdNotIn.setTransferHandler(new MovieListTransferHandler());
        jListNewEditDvdNotIn.setDragEnabled(true);
        jListNewEditDvdNotIn.setDropMode(DropMode.INSERT);
        jScrollPane6.setViewportView(jListNewEditDvdNotIn);

        jSplitPane2.setLeftComponent(jScrollPane6);

        javax.swing.GroupLayout jFrameNewEditDvdLayout = new javax.swing.GroupLayout(jFrameNewEditDvd.getContentPane());
        jFrameNewEditDvd.getContentPane().setLayout(jFrameNewEditDvdLayout);
        jFrameNewEditDvdLayout.setHorizontalGroup(
            jFrameNewEditDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrameNewEditDvdLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jFrameNewEditDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
                    .addGroup(jFrameNewEditDvdLayout.createSequentialGroup()
                        .addComponent(jButtonNewEditDvdMM)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 270, Short.MAX_VALUE)
                        .addComponent(jButtonNewEditDvdCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonNewEditDvdConfirm))
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jFrameNewEditDvdLayout.createSequentialGroup()
                        .addGroup(jFrameNewEditDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelDvdType)
                            .addComponent(jLabelDvdTitle))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jFrameNewEditDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldDvdTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addComponent(jComboBoxDvdType, 0, 435, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jFrameNewEditDvdLayout.setVerticalGroup(
            jFrameNewEditDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrameNewEditDvdLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jFrameNewEditDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldDvdTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelDvdTitle))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jFrameNewEditDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxDvdType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelDvdType))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jFrameNewEditDvdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonNewEditDvdConfirm)
                    .addComponent(jButtonNewEditDvdCancel)
                    .addComponent(jButtonNewEditDvdMM))
                .addContainerGap())
        );

        jFileChooserImport.setCurrentDirectory(new File(System.getProperty("user.home")));
        jFileChooserImport.setDialogTitle("Import from ODS");

        jFileChooserExport.setDialogTitle("Export to ODS");
        jFileChooserExport.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
        jFileChooserExport.setSelectedFile(new java.io.File("/home/vaclav/untitled.ods"));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("VideoDes");
        setMinimumSize(new java.awt.Dimension(600, 400));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jToolBar.setFloatable(false);
        jToolBar.setRollover(true);

        jButtonNewDvd.setText("New");
        jButtonNewDvd.setFocusable(false);
        jButtonNewDvd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonNewDvd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonNewDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewDvdActionPerformed(evt);
            }
        });
        jToolBar.add(jButtonNewDvd);

        jButtonEditDvd.setText("Edit");
        jButtonEditDvd.setEnabled(false);
        jButtonEditDvd.setFocusable(false);
        jButtonEditDvd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonEditDvd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonEditDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditDvdActionPerformed(evt);
            }
        });
        jToolBar.add(jButtonEditDvd);

        jButtonDeleteDvd.setText("Delete");
        jButtonDeleteDvd.setEnabled(false);
        jButtonDeleteDvd.setFocusable(false);
        jButtonDeleteDvd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonDeleteDvd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonDeleteDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteDvdActionPerformed(evt);
            }
        });
        jToolBar.add(jButtonDeleteDvd);
        jToolBar.add(jSeparatorToolBar);

        jButtonDetailDvd.setText("Detail (content)");
        jButtonDetailDvd.setEnabled(false);
        jButtonDetailDvd.setFocusable(false);
        jButtonDetailDvd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonDetailDvd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonDetailDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDetailDvdActionPerformed(evt);
            }
        });
        jToolBar.add(jButtonDetailDvd);
        jToolBar.add(jSeparatorToolBar2);

        jButtonManageMovies.setText("Manage movies");
        jButtonManageMovies.setFocusable(false);
        jButtonManageMovies.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonManageMovies.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonManageMovies.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonManageMoviesActionPerformed(evt);
            }
        });
        jToolBar.add(jButtonManageMovies);

        jTableDvd.setModel(new DvdTableModel());
        jTableDvd.setAutoCreateRowSorter(true);
        jScrollPaneDvdTable.setViewportView(jTableDvd);
        jTableDvd.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent lse) {
                if (!jTableDvd.getSelectionModel().isSelectionEmpty()) {
                    jButtonEditDvd.setEnabled(true);
                    jButtonDeleteDvd.setEnabled(true);
                    jButtonDetailDvd.setEnabled(true);
                    jMenuItemEditDvd.setEnabled(true);
                    jMenuItemDeleteDvd.setEnabled(true);
                    jMenuItemDetailDvd.setEnabled(true);
                    jMenuPopupEditDvd.setEnabled(true);
                    jMenuPopupDeleteDvd.setEnabled(true);
                    jMenuPopupDetailDvd.setEnabled(true);
                } else {
                    jButtonEditDvd.setEnabled(false);
                    jButtonDeleteDvd.setEnabled(false);
                    jButtonDetailDvd.setEnabled(false);
                    jMenuItemEditDvd.setEnabled(false);
                    jMenuItemDeleteDvd.setEnabled(false);
                    jMenuItemDetailDvd.setEnabled(false);
                    jMenuPopupEditDvd.setEnabled(false);
                    jMenuPopupDeleteDvd.setEnabled(false);
                    jMenuPopupDetailDvd.setEnabled(false);
                }
            }
        });

        jTableDvd.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                mousePressed(e);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    int selectedRow = jTableDvd.rowAtPoint(e.getPoint());
                    jTableDvd.setRowSelectionInterval(selectedRow, selectedRow);
                    jPopupMenuTableDvd.show(jTableDvd, e.getX(), e.getY());
                }
            }
        });

        jButtonSearch.setText("Search");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jComboBoxType.setModel(new javax.swing.DefaultComboBoxModel(new Object[] {"ALL", DvdType.COPY, DvdType.HOME, DvdType.MAGAZINE, DvdType.ORIGINAL}));
        jComboBoxType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxTypeActionPerformed(evt);
            }
        });

        jTextFieldSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldSearchActionPerformed(evt);
            }
        });

        jMenuFile.setText("File");

        jMenuItemNewDvd.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemNewDvd.setText("New");
        jMenuItemNewDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNewDvdActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuItemNewDvd);
        jMenuFile.add(jSeparatorFile);

        jMenuItemDetailDvd.setText("Detail (content)");
        jMenuItemDetailDvd.setEnabled(false);
        jMenuItemDetailDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemDetailDvdActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuItemDetailDvd);

        jMenuItemImport.setText("Import DVDs from ODS");
        jMenuItemImport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemImportActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuItemImport);

        jMenuItemExport.setText("Export DVDs to ODS");
        jMenuItemExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExportActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuItemExport);
        jMenuFile.add(jSeparatorFile2);

        jMenuItemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItemExit.setText("Exit");
        jMenuFile.add(jMenuItemExit);

        jMenuBar.add(jMenuFile);

        jMenuEdit.setText("Edit");

        jMenuItemEditDvd.setText("Edit");
        jMenuItemEditDvd.setEnabled(false);
        jMenuItemEditDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemEditDvdActionPerformed(evt);
            }
        });
        jMenuEdit.add(jMenuItemEditDvd);

        jMenuItemDeleteDvd.setText("Delete");
        jMenuItemDeleteDvd.setEnabled(false);
        jMenuItemDeleteDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemDeleteDvdActionPerformed(evt);
            }
        });
        jMenuEdit.add(jMenuItemDeleteDvd);

        jMenuBar.add(jMenuEdit);

        setJMenuBar(jMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jComboBoxType, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSearch)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPaneDvdTable, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSearch)
                    .addComponent(jComboBoxType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPaneDvdTable, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        XMLDBTemplate.close();
    }//GEN-LAST:event_formWindowClosing

    private class RetrieveMovies extends SwingWorker<List<Movie>, Void> {

        @Override
        protected List<Movie> doInBackground() throws Exception {
            List<Movie> movies = movieManager.getAllMovies();

            return movies;
        }

        @Override
        protected void done() {
            try {
                List<Movie> movies = get();
                MovieTableModel tableModel = new MovieTableModel(movies);

                jTableMovie.setModel(tableModel);
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "An error occured when retrieving dvds. ", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "An error occured when retrieving dvds. ", ex);
            }
        }
    }

    private class RetrieveDvds extends SwingWorker<List<Dvd>, Void> {

        @Override
        protected List<Dvd> doInBackground() throws Exception {
            List<Dvd> dvds = null;

            if (jComboBoxType.getSelectedItem().equals("ALL")) {
                dvds = dvdManager.getAllDvds();
            } else {
                dvds = dvdManager.getDvdsByType((DvdType) jComboBoxType.getSelectedItem());
            }

            return dvds;
        }

        @Override
        protected void done() {
            try {
                List<Dvd> dvds = get();
                DvdTableModel tableModel = new DvdTableModel(dvds);

                jTableDvd.setModel(tableModel);
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "An error occured when retrieving dvds. ", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "An error occured when retrieving dvds. ", ex);
            }
        }
    }

    private void jComboBoxTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxTypeActionPerformed
        new RetrieveDvds().execute();
    }//GEN-LAST:event_jComboBoxTypeActionPerformed

    private class SearchDvd extends SwingWorker<List<Dvd>, Void> {

        @Override
        protected List<Dvd> doInBackground() throws Exception {
            List<Dvd> allDvds = null;
            if (jComboBoxType.getSelectedItem().equals("ALL")) {
                allDvds = dvdManager.getAllDvds();
            } else {
                allDvds = dvdManager.getDvdsByType((DvdType) jComboBoxType.getSelectedItem());
            }

            List<Dvd> matchesDvds = new ArrayList<Dvd>();
            String query = jTextFieldSearch.getText();

            for (Dvd d : allDvds) {
                if (d.getTitle().matches("(?i).*" + query + ".*")) {
                    matchesDvds.add(d);
                }
            }

            return matchesDvds;
        }

        @Override
        protected void done() {
            try {
                List<Dvd> matchesDvds = get();
                DvdTableModel tableModel = new DvdTableModel(matchesDvds);

                jTableDvd.setModel(tableModel);
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "Error when searching dvd. ", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "Error when searching dvd. ", ex);
            }
        }
    }

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        new SearchDvd().execute();
    }//GEN-LAST:event_jButtonSearchActionPerformed

    private void jTextFieldSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldSearchActionPerformed
        jButtonSearchActionPerformed(evt);
    }//GEN-LAST:event_jTextFieldSearchActionPerformed

    private class DeleteDvd extends SwingWorker<List<Dvd>, Void> {

        @Override
        protected List<Dvd> doInBackground() throws Exception {
            int[] dvdIndexes = jTableDvd.getSelectedRows();

            List<Dvd> dvdsToBeDeleted = new ArrayList<Dvd>();
            for (Integer i : dvdIndexes) {
                Dvd dvd = new Dvd();
                dvd.setId((String) jTableDvd.getValueAt(i, -1));

                dvdManager.remove(dvd);
                dvdsToBeDeleted.add(dvd);
            }

            return dvdsToBeDeleted;
        }

        @Override
        protected void done() {
            try {
                List<Dvd> dvds = get();
                DvdTableModel tableModel = (DvdTableModel) jTableDvd.getModel();

                for (Dvd d : dvds) {
                    tableModel.remove(d);
                }
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "An error when deleting dvd(s). ", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "An error when deleting dvd(s). ", ex);
            }
        }
    }

    private void jMenuItemDeleteDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDeleteDvdActionPerformed
        String message = "Do you really want to delete selected DVD ?";
        String title = "Delete DVD ?";
        if (jTableDvd.getSelectedRowCount() > 1) {
            message = "Do you really want to delete selected DVDs ?";
            title = "Delte DVDs ?";
        }

        int option = JOptionPane.showConfirmDialog(rootPane, message, title, JOptionPane.YES_NO_OPTION);
        if (option == JOptionPane.OK_OPTION) {
            new DeleteDvd().execute();
        }
    }//GEN-LAST:event_jMenuItemDeleteDvdActionPerformed

    private void jButtonDeleteDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteDvdActionPerformed
        jMenuItemDeleteDvdActionPerformed(evt);
    }//GEN-LAST:event_jButtonDeleteDvdActionPerformed

    private void jMenuPopupDeleteDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPopupDeleteDvdActionPerformed
        jMenuItemDeleteDvdActionPerformed(evt);
    }//GEN-LAST:event_jMenuPopupDeleteDvdActionPerformed

    private void jButtonManageMoviesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonManageMoviesActionPerformed
        jFrameManageMovies.setVisible(true);
    }//GEN-LAST:event_jButtonManageMoviesActionPerformed

    private class DeleteMovie extends SwingWorker<List<Movie>, Void> {

        @Override
        protected List<Movie> doInBackground() throws Exception {
            int[] movieIndexes = jTableMovie.getSelectedRows();

            List<Movie> moviesToBeDeleted = new ArrayList<Movie>();
            for (Integer i : movieIndexes) {
                Movie movie = (Movie) jTableMovie.getValueAt(i, -2);

                movieManager.remove(movie);
                moviesToBeDeleted.add(movie);
            }

            return moviesToBeDeleted;
        }

        @Override
        protected void done() {
            try {
                List<Movie> movies = get();
                MovieTableModel tableModel = (MovieTableModel) jTableMovie.getModel();

                for (Movie m : movies) {
                    tableModel.remove(m);
                }
                new RetrieveDvds().execute();
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "An error when deleting dvd(s). ", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "An error when deleting dvd(s). ", ex);
            }
        }
    }

    private void jButtonDeleteMovieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteMovieActionPerformed
        String message = "Do you really want to delete selected movie (Content of DVDs that contain this movie will be changed) ?";
        String title = "Delete movie ?";
        if (jTableMovie.getSelectedRowCount() > 1) {
            message = "Do you really want to delete selected movies (Content of DVDs that contain these movies will be changed) ?";
            title = "Delte movies ?";
        }

        int option = JOptionPane.showConfirmDialog(jFrameManageMovies, message, title, JOptionPane.YES_NO_OPTION);
        if (option == JOptionPane.OK_OPTION) {
            new DeleteMovie().execute();
        }
    }//GEN-LAST:event_jButtonDeleteMovieActionPerformed

    private void jMenuPopupDeleteMovieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPopupDeleteMovieActionPerformed
        jButtonDeleteMovieActionPerformed(evt);
    }//GEN-LAST:event_jMenuPopupDeleteMovieActionPerformed

    private void jButtonMMCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMMCloseActionPerformed
        jFrameManageMovies.setVisible(false);
    }//GEN-LAST:event_jButtonMMCloseActionPerformed

    private void jButtonNewMovieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewMovieActionPerformed
        jTextFieldMovieName.setText("");
        jTextFieldMovieDirector.setText("");
        jSpinnerMovieYear.setValue(2000);
        jTextFieldMovieCountry.setText("");
        jListMovieActors.setModel(new ActorListModel());

        GenreListModel jListModel = new GenreListModel();

        for (Genre g : Genre.values()) {
            jListModel.add(g);
        }
        jListMovieGenresNotIn.setModel(jListModel);
        jListMovieGenresIn.setModel(new GenreListModel());

        jButtonMovieConfirm.setText("Create");

        jFrameNewEditMovie.setVisible(true);
    }//GEN-LAST:event_jButtonNewMovieActionPerformed

    private void jMenuPopupNewMovieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPopupNewMovieActionPerformed
        jButtonNewMovieActionPerformed(evt);
    }//GEN-LAST:event_jMenuPopupNewMovieActionPerformed

    private void jButtonMovieAddActorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMovieAddActorActionPerformed
        String actor = JOptionPane.showInputDialog(jFrameNewEditMovie, "Add actor", "Actor name: ", JOptionPane.OK_CANCEL_OPTION);
        ((ActorListModel) jListMovieActors.getModel()).add(actor);
    }//GEN-LAST:event_jButtonMovieAddActorActionPerformed

    private void jButtonMovieDeleteActorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMovieDeleteActorActionPerformed
        ((ActorListModel) jListMovieActors.getModel()).remove((String) jListMovieActors.getSelectedValue());
    }//GEN-LAST:event_jButtonMovieDeleteActorActionPerformed

    private void jButtonMovieCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMovieCloseActionPerformed
        jFrameNewEditMovie.setVisible(false);
    }//GEN-LAST:event_jButtonMovieCloseActionPerformed

    private class CreateMovie extends SwingWorker<Movie, Void> {

        @Override
        protected Movie doInBackground() throws Exception {
            Movie movie = new Movie();

            movie.setName(jTextFieldMovieName.getText());
            movie.setDirector(jTextFieldMovieDirector.getText());
            movie.setYear((Integer) jSpinnerMovieYear.getValue());
            movie.setCountry(jTextFieldMovieCountry.getText());


            List<String> actorsList = ((ActorListModel) jListMovieActors.getModel()).getAllActors();
            Set<String> actors = new HashSet<String>();

            for (String s : actorsList) {
                actors.add(s);
            }

            movie.setActors(actors);

            List<Genre> genresList = ((GenreListModel) jListMovieGenresIn.getModel()).getAllGenres();
            Set<Genre> genres = new HashSet<Genre>();

            for (Genre g : genresList) {
                genres.add(g);
            }

            movie.setGenres(genres);

            movieManager.add(movie);

            return movie;
        }

        @Override
        protected void done() {
            try {
                Movie movie = get();
                ((MovieTableModel) jTableMovie.getModel()).add(movie);
                ((MovieListModel) jListNewEditDvdNotIn.getModel()).add(movie);
                jFrameNewEditMovie.setVisible(false);
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "An error occurd when creating a movie. ", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "An error occurd when creating a movie. ", ex);
            }
        }
    }

    private void jButtonMovieConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMovieConfirmActionPerformed
        if (jTextFieldMovieName.getText().isEmpty()) {
            JOptionPane.showMessageDialog(
                    jFrameNewEditMovie, "The name of the movie must me specified.", "Missing movie name", JOptionPane.ERROR_MESSAGE);
        } else {
            if (jButtonMovieConfirm.getText().equals("Create")) {
                new CreateMovie().execute();
            } else {
                new EditMovie().execute();
            }
        }
    }//GEN-LAST:event_jButtonMovieConfirmActionPerformed

    private class EditMovie extends SwingWorker<Movie, Void> {

        @Override
        protected Movie doInBackground() throws Exception {
            Movie movie = new Movie();
            movie.setId((String) jTableMovie.getValueAt(jTableMovie.getSelectedRow(), -1));

            movie.setName(jTextFieldMovieName.getText());
            movie.setDirector(jTextFieldMovieDirector.getText());
            movie.setYear((Integer) jSpinnerMovieYear.getValue());
            movie.setCountry(jTextFieldMovieCountry.getText());


            List<String> actorsList = ((ActorListModel) jListMovieActors.getModel()).getAllActors();
            Set<String> actors = new HashSet<String>();

            for (String s : actorsList) {
                actors.add(s);
            }

            movie.setActors(actors);

            List<Genre> genresList = ((GenreListModel) jListMovieGenresIn.getModel()).getAllGenres();
            Set<Genre> genres = new HashSet<Genre>();

            for (Genre g : genresList) {
                genres.add(g);
            }

            movie.setGenres(genres);

            movieManager.update(movie);

            return movie;
        }

        @Override
        protected void done() {
            try {
                Movie movie = get();
                ((MovieTableModel) jTableMovie.getModel()).update(movie);
                ((MovieListModel) jListNewEditDvdNotIn.getModel()).update(movie);
                new RetrieveDvds().execute();
                jFrameNewEditMovie.setVisible(false);
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "An error occurd when editing a movie. ", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "An error occurd when editing a movie. ", ex);
            }
        }
    }

    private void jButtonEditMovieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditMovieActionPerformed
        Movie movie = (Movie) jTableMovie.getValueAt(jTableMovie.getSelectedRow(), -2);

        jTextFieldMovieName.setText(movie.getName());
        jTextFieldMovieDirector.setText(movie.getDirector());
        if (!(movie.getYear() == null)) {
            jSpinnerMovieYear.setValue(movie.getYear());
        } else {
            jSpinnerMovieYear.setValue(2000);
        }
        jTextFieldMovieCountry.setText(movie.getCountry());

        ActorListModel actorModel = new ActorListModel();
        for (String s : movie.getActors()) {
            actorModel.add(s);
        }
        jListMovieActors.setModel(actorModel);

        GenreListModel genreModelIn = new GenreListModel();
        GenreListModel genreModelNotIn = new GenreListModel();

        Set<Genre> genresIn = movie.getGenres();
        for (Genre g : Genre.values()) {
            if (genresIn.contains(g)) {
                genreModelIn.add(g);
            } else {
                genreModelNotIn.add(g);
            }
        }
        jListMovieGenresNotIn.setModel(genreModelNotIn);
        jListMovieGenresIn.setModel(genreModelIn);

        jButtonMovieConfirm.setText("Edit");

        jFrameNewEditMovie.setVisible(true);
    }//GEN-LAST:event_jButtonEditMovieActionPerformed

    private void jMenuPopupEditMovieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPopupEditMovieActionPerformed
        jButtonEditMovieActionPerformed(evt);
    }//GEN-LAST:event_jMenuPopupEditMovieActionPerformed

    private void jButtonDetailDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDetailDvdActionPerformed
        Set<Movie> movies = (Set<Movie>) jTableDvd.getValueAt(jTableDvd.getSelectedRow(), -2);

        MovieTableModel tableModel = new MovieTableModel();
        for (Movie m : movies) {
            tableModel.add(m);
        }

        jTableDetailDvd.setModel(tableModel);

        jFrameDetailDvd.setTitle((String) jTableDvd.getValueAt(jTableDvd.getSelectedRow(), 0));

        jFrameDetailDvd.setVisible(true);
    }//GEN-LAST:event_jButtonDetailDvdActionPerformed

    private void jButtonDetailDvdCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDetailDvdCloseActionPerformed
        jFrameDetailDvd.setVisible(false);
    }//GEN-LAST:event_jButtonDetailDvdCloseActionPerformed

    private void jMenuItemDetailDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDetailDvdActionPerformed
        jButtonDetailDvdActionPerformed(evt);
    }//GEN-LAST:event_jMenuItemDetailDvdActionPerformed

    private void jMenuPopupDetailDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPopupDetailDvdActionPerformed
        jButtonDetailDvdActionPerformed(evt);
    }//GEN-LAST:event_jMenuPopupDetailDvdActionPerformed

    private void jButtonNewDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewDvdActionPerformed
        jTextFieldDvdTitle.setText("");
        jComboBoxDvdType.setSelectedIndex(0);

        List<Movie> moviesNotIn = new ArrayList<Movie>();
        moviesNotIn.addAll(((MovieTableModel) jTableMovie.getModel()).getAllMovies());
        MovieListModel listModelNotIn = new MovieListModel(moviesNotIn);
        MovieListModel listModelIn = new MovieListModel();

        jListNewEditDvdNotIn.setModel(listModelNotIn);
        jListNewEditDvdIn.setModel(listModelIn);

        jButtonNewEditDvdConfirm.setText("Create");

        jFrameNewEditDvd.setVisible(true);
    }//GEN-LAST:event_jButtonNewDvdActionPerformed

    private void jMenuItemNewDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNewDvdActionPerformed
        jButtonNewDvdActionPerformed(evt);
    }//GEN-LAST:event_jMenuItemNewDvdActionPerformed

    private void jMenuPopupNewDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPopupNewDvdActionPerformed
        jButtonNewDvdActionPerformed(evt);
    }//GEN-LAST:event_jMenuPopupNewDvdActionPerformed

    private void jButtonNewEditDvdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewEditDvdCancelActionPerformed
        jFrameNewEditDvd.setVisible(false);
    }//GEN-LAST:event_jButtonNewEditDvdCancelActionPerformed

    private void jButtonEditDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditDvdActionPerformed
        Dvd dvd = (Dvd) jTableDvd.getValueAt(jTableDvd.getSelectedRow(), -3);

        jTextFieldDvdTitle.setText(dvd.getTitle());
        jComboBoxDvdType.setSelectedItem(dvd.getType());

        List<Movie> moviesNotIn = new ArrayList<Movie>();
        List<Movie> moviesIn = new ArrayList<Movie>();
        List<Movie> allMovies = new ArrayList<Movie>();
        allMovies.addAll(((MovieTableModel) jTableMovie.getModel()).getAllMovies());
        moviesIn.addAll(dvd.getContent());

        for (Movie m : allMovies) {
            if (!moviesIn.contains(m)) {
                moviesNotIn.add(m);
            }
        }

        MovieListModel listModelNotIn = new MovieListModel(moviesNotIn);
        MovieListModel listModelIn = new MovieListModel(moviesIn);

        jListNewEditDvdNotIn.setModel(listModelNotIn);
        jListNewEditDvdIn.setModel(listModelIn);

        jButtonNewEditDvdConfirm.setText("Edit");

        jFrameNewEditDvd.setVisible(true);
    }//GEN-LAST:event_jButtonEditDvdActionPerformed

    private void jMenuItemEditDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemEditDvdActionPerformed
        jButtonEditDvdActionPerformed(evt);
    }//GEN-LAST:event_jMenuItemEditDvdActionPerformed

    private void jMenuPopupEditDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPopupEditDvdActionPerformed
        jButtonEditDvdActionPerformed(evt);
    }//GEN-LAST:event_jMenuPopupEditDvdActionPerformed

    private void jButtonNewEditDvdMMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewEditDvdMMActionPerformed
        jButtonManageMoviesActionPerformed(evt);
    }//GEN-LAST:event_jButtonNewEditDvdMMActionPerformed

    private class CreateDvd extends SwingWorker<Dvd, Void> {

        @Override
        protected Dvd doInBackground() throws Exception {
            Dvd dvd = new Dvd();
            dvd.setTitle(jTextFieldDvdTitle.getText());
            dvd.setType((DvdType) jComboBoxDvdType.getSelectedItem());

            List<Movie> moviesList = ((MovieListModel) jListNewEditDvdIn.getModel()).getAllMovies();
            Set<Movie> movies = new HashSet<Movie>();

            for (Movie m : moviesList) {
                movies.add(m);
            }

            dvd.setContent(movies);

            dvdManager.add(dvd);

            return dvd;
        }

        @Override
        protected void done() {
            try {
                ((DvdTableModel) jTableDvd.getModel()).add(get());
                jFrameNewEditDvd.setVisible(false);
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "Error when creating new DVD. ", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "Error when creating new DVD. ", ex);
            }
        }
    }

    private void jButtonNewEditDvdConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewEditDvdConfirmActionPerformed
        if (jTextFieldDvdTitle.getText().isEmpty()) {
            JOptionPane.showMessageDialog(
                    jFrameNewEditDvd, "The title of the DVD must me specified.", "Missing DVD title", JOptionPane.ERROR_MESSAGE);
        } else {
            if (jButtonNewEditDvdConfirm.getText().equals("Create")) {
                new CreateDvd().execute();
            } else {
                new EditDvd().execute();
            }
        }
    }//GEN-LAST:event_jButtonNewEditDvdConfirmActionPerformed

    private class ImportMedia extends SwingWorker<Integer, Void> {

        @Override
        protected Integer doInBackground() throws Exception {
            File file = jFileChooserImport.getSelectedFile();

            try {
                ImportFromODS ifo = ImportFromODS.newInstance(file);

                ifo.importToDatabase();
                return 0;
            } catch (ImportFailedException ex) {
                LOGGER.log(Level.INFO, "Import failed.", ex);
                return 1;
            } catch (Exception ex) {
                LOGGER.log(Level.INFO, "Import failed. Odf file couldn't be loaded.", ex);
                return 2;
            }
        }

        @Override
        protected void done() {
            try {
                int error = get();
                if (error == 0) {
                    new RetrieveMovies().execute();
                    new RetrieveDvds().execute();
                } else if (error == 1) {
                    JOptionPane.showMessageDialog(
                            rootPane, "Import failed. The format of chosen ODF is improper.", "Import failed.", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(
                            rootPane, "Import failed. The chosen ODF couldn't be loaded.", "Import failed.", JOptionPane.ERROR_MESSAGE);
                }
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "Error when importing data to the database.", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "Error when importing data to the database.", ex);
            }
        }
    }

    private void jMenuItemImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemImportActionPerformed
        int returnVal = jFileChooserImport.showOpenDialog(rootPane);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            new ImportMedia().execute();
        }
    }//GEN-LAST:event_jMenuItemImportActionPerformed

    private class ExportMedia extends SwingWorker<Integer, Void> {

        @Override
        protected Integer doInBackground() throws Exception {
            File file = jFileChooserExport.getSelectedFile();
            
            try {
                ExportToODS.execute(file, dvdManager.getAllDvds(), movieManager.getAllMovies());
                return 0;
            } catch (Exception ex) {
                LOGGER.log(Level.INFO, "Export failed. Odf file couldn't be created.", ex);
                return 1;
            }
        }

        @Override
        protected void done() {
            try {
                int error = get();
                if (error == 0) {
                    new RetrieveMovies().execute();
                    new RetrieveDvds().execute();
                } else {
                    JOptionPane.showMessageDialog(
                        rootPane, "Export failed. Odf file couldn't be created.", "Export failed.", JOptionPane.ERROR_MESSAGE);
                }
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "Error when importing data to the database.", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "Error when importing data to the database.", ex);
            }
        }
    }
    private void jMenuItemExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExportActionPerformed
        int returnVal = jFileChooserExport.showSaveDialog(rootPane);
        
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            new ExportMedia().execute();
        }
    }//GEN-LAST:event_jMenuItemExportActionPerformed

    private class EditDvd extends SwingWorker<Dvd, Void> {

        @Override
        protected Dvd doInBackground() throws Exception {
            Dvd dvd = new Dvd();
            dvd.setId((String) jTableDvd.getValueAt(jTableDvd.getSelectedRow(), -1));
            dvd.setTitle(jTextFieldDvdTitle.getText());
            dvd.setType((DvdType) jComboBoxDvdType.getSelectedItem());

            List<Movie> moviesList = ((MovieListModel) jListNewEditDvdIn.getModel()).getAllMovies();
            Set<Movie> movies = new HashSet<Movie>();

            for (Movie m : moviesList) {
                movies.add(m);
            }

            dvd.setContent(movies);

            dvdManager.update(dvd);

            return dvd;
        }

        @Override
        protected void done() {
            try {
                ((DvdTableModel) jTableDvd.getModel()).update(get());
                jFrameNewEditDvd.setVisible(false);
            } catch (InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "Error when creating new DVD. ", ex);
            } catch (ExecutionException ex) {
                LOGGER.log(Level.SEVERE, "Error when creating new DVD. ", ex);
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
	URL log4j = VideoDes.class.getClassLoader().getResource("cz/muni/fi/pb138/videodes/files/log4j.xml");
	
	createDirectoryIfDoesntExist("data");
	createDirectoryIfDoesntExist("logs");
	DOMConfigurator.configure(log4j);
	
        java.awt.EventQueue.invokeLater(new Runnable() {	    
            @Override
            public void run() {
                new VideoDes().setVisible(true);
            }
        });
    }
    // <editor-fold defaultstate="collapsed" desc="Java swing variables declaration">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonDeleteDvd;
    private javax.swing.JButton jButtonDeleteMovie;
    private javax.swing.JButton jButtonDetailDvd;
    private javax.swing.JButton jButtonDetailDvdClose;
    private javax.swing.JButton jButtonEditDvd;
    private javax.swing.JButton jButtonEditMovie;
    private javax.swing.JButton jButtonMMClose;
    private javax.swing.JButton jButtonManageMovies;
    private javax.swing.JButton jButtonMovieAddActor;
    private javax.swing.JButton jButtonMovieClose;
    private javax.swing.JButton jButtonMovieConfirm;
    private javax.swing.JButton jButtonMovieDeleteActor;
    private javax.swing.JButton jButtonNewDvd;
    private javax.swing.JButton jButtonNewEditDvdCancel;
    private javax.swing.JButton jButtonNewEditDvdConfirm;
    private javax.swing.JButton jButtonNewEditDvdMM;
    private javax.swing.JButton jButtonNewMovie;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JComboBox jComboBoxDvdType;
    private javax.swing.JComboBox jComboBoxType;
    private javax.swing.JFileChooser jFileChooserExport;
    private javax.swing.JFileChooser jFileChooserImport;
    private javax.swing.JFrame jFrameDetailDvd;
    private javax.swing.JFrame jFrameManageMovies;
    private javax.swing.JFrame jFrameNewEditDvd;
    private javax.swing.JFrame jFrameNewEditMovie;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelDvdTitle;
    private javax.swing.JLabel jLabelDvdType;
    private javax.swing.JLabel jLabelMovieActors;
    private javax.swing.JLabel jLabelMovieCountry;
    private javax.swing.JLabel jLabelMovieDirector;
    private javax.swing.JLabel jLabelMovieGenres;
    private javax.swing.JLabel jLabelMovieName;
    private javax.swing.JLabel jLabelMovieYear;
    private javax.swing.JList jListMovieActors;
    private javax.swing.JList jListMovieGenresIn;
    private javax.swing.JList jListMovieGenresNotIn;
    private javax.swing.JList jListNewEditDvdIn;
    private javax.swing.JList jListNewEditDvdNotIn;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenu jMenuEdit;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenuItem jMenuItemDeleteDvd;
    private javax.swing.JMenuItem jMenuItemDetailDvd;
    private javax.swing.JMenuItem jMenuItemEditDvd;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemExport;
    private javax.swing.JMenuItem jMenuItemImport;
    private javax.swing.JMenuItem jMenuItemNewDvd;
    private javax.swing.JMenuItem jMenuPopupDeleteDvd;
    private javax.swing.JMenuItem jMenuPopupDeleteMovie;
    private javax.swing.JMenuItem jMenuPopupDetailDvd;
    private javax.swing.JMenuItem jMenuPopupEditDvd;
    private javax.swing.JMenuItem jMenuPopupEditMovie;
    private javax.swing.JMenuItem jMenuPopupNewDvd;
    private javax.swing.JMenuItem jMenuPopupNewMovie;
    private javax.swing.JPopupMenu jPopupMenuTableDvd;
    private javax.swing.JPopupMenu jPopupMenuTableMovie;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPaneDvdTable;
    private javax.swing.JScrollPane jScrollPaneMovieTable;
    private javax.swing.JPopupMenu.Separator jSeparatorFile;
    private javax.swing.JPopupMenu.Separator jSeparatorFile2;
    private javax.swing.JPopupMenu.Separator jSeparatorPopup;
    private javax.swing.JPopupMenu.Separator jSeparatorPopup2;
    private javax.swing.JPopupMenu.Separator jSeparatorPopupMM;
    private javax.swing.JToolBar.Separator jSeparatorToolBar;
    private javax.swing.JToolBar.Separator jSeparatorToolBar2;
    private javax.swing.JSpinner jSpinnerMovieYear;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTable jTableDetailDvd;
    private javax.swing.JTable jTableDvd;
    private javax.swing.JTable jTableMovie;
    private javax.swing.JTextField jTextFieldDvdTitle;
    private javax.swing.JTextField jTextFieldMovieCountry;
    private javax.swing.JTextField jTextFieldMovieDirector;
    private javax.swing.JTextField jTextFieldMovieName;
    private javax.swing.JTextField jTextFieldSearch;
    private javax.swing.JToolBar jToolBar;
    private javax.swing.JToolBar jToolBarMM;
    // End of variables declaration//GEN-END:variables
//</editor-fold>
}
